package com.ruoyi.flowable.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.flowable.domain.Endbilltrace;
import com.ruoyi.flowable.domain.dto.FlowTaskDto;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.flowable.domain.EndedBill;
import com.ruoyi.flowable.service.IEndedBillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

/**
 * 已完成的工作票Controller
 *
 * @author ruoyi
 * @date 2022-07-25
 */
@RestController
@RequestMapping("/flowable/EndedBill")
public class EndedBillController extends BaseController {
    @Autowired
    private IEndedBillService endedBillService;

    /**
     * 查询已完成的工作票列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:EndedBill:list')")
    @GetMapping("/list")
    public TableDataInfo list(EndedBill endedBill) {
        startPage();
        List<EndedBill> list = endedBillService.selectEndedBillList(endedBill);
        return getDataTable(list);
    }

    /**
     * 导出已完成的工作票列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:EndedBill:export')")
    @Log(title = "已完成的工作票", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EndedBill endedBill) {
        List<EndedBill> list = endedBillService.selectEndedBillList(endedBill);
        ExcelUtil<EndedBill> util = new ExcelUtil<EndedBill>(EndedBill.class);
        return util.exportExcel(list, "EndedBill");
    }


//    /**
//     * 查询工作票流程
//     */
//    @PreAuthorize("@ss.hasPermi('process:EndedBill:add')")
//    @Log(title = "已完成的工作票", businessType = BusinessType.INSERT)
//    @PostMapping("/getTrace")
//    public AjaxResult add(@RequestBody EndedBill endedBill)
//    {
//        List<Endbilltrace> list = endedBillService.selectEndedBillTraceList(endedBill);
//        return AjaxResult.success(list);
//    }

    /**
     * 获取已完成的工作票详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowable:EndedBill:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId) {
        return AjaxResult.success(endedBillService.selectEndedBillById(workId));
    }

//    /**
//     * 新增已完成的工作票
//     */
//    @PreAuthorize("@ss.hasPermi('flowable:EndedBill:add')")
//    @Log(title = "已完成的工作票", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody EndedBill endedBill)
//    {
//        return toAjax(endedBillService.insertEndedBill(endedBill));
//    }

    /**
     * 查询工作票流程
     */
    @PreAuthorize("@ss.hasPermi('process:EndedBill:add')")
    @Log(title = "已完成的工作票", businessType = BusinessType.INSERT)
    @PostMapping("/getTrace")
    public AjaxResult add1(@RequestBody JSONObject jsonObject)
    {
        String worksheetcode = jsonObject.getString("mainBizKey");
        EndedBill endedBill = new EndedBill();
        endedBill.setWorkSheetCode(worksheetcode);
        List<Endbilltrace> list = endedBillService.selectEndedBillTraceList(endedBill);
        return AjaxResult.success(list);
    }




    /**
     * 流程图
     */
    @PreAuthorize("@ss.hasPermi('process:EndedBill:readXml')")
    @Log(title = "流程图", businessType = BusinessType.INSERT)
    @PostMapping("/readXml")
    public AjaxResult readXml(@RequestBody String workSheetCode)
    {
//        String worksheetcode = jsonObject.getString("mainBizKey");
        EndedBill endedBill = new EndedBill();
        endedBill.setWorkSheetCode(workSheetCode);
        List<Endbilltrace> list = endedBillService.selectEndedBillTraceList(endedBill);
        try {
            return endedBillService.readXml(endedBill);
        } catch (Exception e) {
            return AjaxResult.error("加载xml文件异常");
        }
    }


    @ApiOperation(value = "读取图片文件")
    @GetMapping("/readImage/{deployId}")
    public void readImage(@ApiParam(value = "流程定义id") @PathVariable(value = "deployId") String deployId, HttpServletResponse response) {
        OutputStream os = null;
        BufferedImage image = null;
        try {
            image = ImageIO.read(endedBillService.readImage(deployId));
            response.setContentType("image/png");
            os = response.getOutputStream();
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }



    /**
     * 修改已完成的工作票
     */
    @PreAuthorize("@ss.hasPermi('flowable:EndedBill:edit')")
    @Log(title = "已完成的工作票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EndedBill endedBill) {
        return toAjax(endedBillService.updateEndedBill(endedBill));
    }

    /**
     * 删除已完成的工作票
     */
    @PreAuthorize("@ss.hasPermi('flowable:EndedBill:remove')")
    @Log(title = "已完成的工作票", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds) {
        return toAjax(endedBillService.deleteEndedBillByIds(workIds));
    }
}








